package com.donglan.pethome.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.donglan.pethome.basic.util.AjaxResult;
import com.donglan.pethome.basic.util.RedisUtil;
import com.donglan.pethome.user.domain.User;
import com.donglan.pethome.user.domain.vo.UserVo;
import com.donglan.pethome.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: zhangdonghui
 * @Date: 2022/1/14 16:41
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /*
     * 功能描述 校验手机号码是否存在
     * @author zhangdonghui
     * @date 2022/1/14 16:43
     * @param param 【既可以使用对象接收，也可以使用Map接收】
     * @return com.donglan.pethome.basic.util.AjaxResult
     */
    @PostMapping("/checkPhone")
    public AjaxResult checkPhone(@RequestBody Map<String,String> param){
        try {
            //获取手机号码
            String phone = param.get("phone");
            //校验手机号码
            userService.checkPhone(phone);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /*
     * 功能描述 发送手机验证码
     * @author zhangdonghui
     * @date 2022/1/14 17:31
     * @param param
     * @return AjaxResult
     */
    @PostMapping("/sendMobileCode")
    public AjaxResult sendMobileCode(@RequestBody Map<String,String> param){
        try {
            //获取前端传递的手机验证码
            String phone = param.get("phone");
            //发送手机验证码
            userService.sendMobileCodeByPhone(phone);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /*
     * 功能描述 手机短信注册
     * @author zhangdonghui
     * @date 2022/1/18 16:23
     * @param userVo
     * @return com.donglan.pethome.basic.util.AjaxResult
     */
    @PutMapping("/register")
    public AjaxResult register(@RequestBody UserVo userVo){
        try {
            userService.register(userVo);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /*
     * 功能描述 登录功能的完成
     * @author zhangdonghui
     * @date 2022/1/20 13:35
     * @param user
     * @return com.donglan.pethome.basic.util.AjaxResult
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){
        try {
            // 通过用户名查询登录用户
            User loginUser = userService.loadByUsernameAndPassword(user.getUsername(),user.getPassword());

            // 1、随机产生redis的key值
            String token = UUID.randomUUID().toString();
            // 2、把登录用户存储到redis中
            // value值需要的类型是String，切记不可使用 loginUser.toString，后期我们获取value的时候解析很麻烦
            // 所以我们将loginUser转成json格式的即可，使用的是alibaba的fastJson
            RedisUtil.INSTANCE.set(token, JSONObject.toJSONString(loginUser),60*30);
            Map<String,Object> map = new HashMap<>();
            // 3、把key值存储起来
            map.put("token",token);
            // 4、把对象存储起来
            map.put("loginUser",loginUser);

            return new AjaxResult().setResultObj(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
}
